import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
/*
 * Created by JFormDesigner on Sun Nov 28 11:59:48 CST 2021
 */



/**
 * @author 123
 */
public class StudentManager extends JFrame {
    Connection conn = null;
    InitialParam initial = null;
    Vector<Vector<String>> students= null;
    Vector<Vector<String>> courses= null;
    Vector<Vector<String>> scs= null;
    Vector<String> columnNames = null;
    DefaultTableModel model = null;

    public StudentManager() {
        setVisible(true);
        initComponents();
        setButtonFalse();
        initialConn();
    }


    void initialConn(){
        try {
            initial = new InitialParam();
            conn = initial.getConn();
            setInformation();
            getStudentSelect();
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }

    void getStudentSelect(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    int count = student_table.getSelectedRow();
                    int count1 = course_table.getSelectedRow();
                    int count2 = sc_table.getSelectedRow();
                    if (count!=-1||count1!=-1||count2!=-1) {
                       setButtonTrue();
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


    void setButtonTrue(){
        delete_button.setEnabled(true);
        modify_button.setEnabled(true);
    }

    void setButtonFalse(){
        delete_button.setEnabled(false);
        modify_button.setEnabled(false);
    }

    void setInformation(){
        try {
            if (conn == null) {
                JOptionPane.showConfirmDialog(null, "获取数据失败。正在连接服务器,请稍后重试！", "提示", JOptionPane.CLOSED_OPTION);
            } else {
                columnNames = new Vector();
                //表头用一个vector容器存放
                columnNames.add("学号");
                columnNames.add("姓名");
                columnNames.add("性别");
                columnNames.add("出生日期");
                columnNames.add("专业号");
                columnNames.add("年级");
                students = initial.getStudent(conn);//数据用vector<vector<>>存放

                //*********更新table表操作***********
//                student_table = new JTable(students,columnNames);
//                scrollPane1.setViewportView(student_table);

                model=new DefaultTableModel(columnNames,0);  //添加首行元素数据
                student_table=new JTable(model){
                    public boolean isCellEditable(int row, int column)
                    {  //使表格不能被编辑
                        return false;
                    }
                };
                model.setDataVector(students,columnNames);
                scrollPane1.setViewportView(student_table);
                //*********更新table表操作***********


                columnNames.clear();
                //表头用一个vector容器存放
                columnNames.add("课程号");
                columnNames.add("PCNO");
                columnNames.add("课程名");
                columnNames.add("年级");
                columnNames.add("学分");
                courses = initial.getCourse(conn);//数据用vector<vector<>>存放

                //*********更新table表操作***********
                course_table = new JTable(courses,columnNames);
                scrollPane2.setViewportView(course_table);
                //*********更新table表操作***********

                student_table.getSelectionModel().addListSelectionListener((new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (!e.getValueIsAdjusting()) {
                              int row=student_table.getSelectedRow();//选中行
                              //int col=table.getSelectedColumn();//选中列
                              System.out.println("方法一:"+student_table.getValueAt(row, 0)+"\t"+student_table.getValueAt(row, 1));
                            /*
                                //支持拖动多选
                                int[] rows = student_table.getSelectedRows();
                                // int[] cols = table.getSelectedColumns();//选中的列
                                for (int i : rows) {
                                    System.out.println("#方法一:\t" + student_table.getValueAt(i, 0) + "\t" + student_table.getValueAt(i, 1));
                                }
                             */
                        }
                    }
                }));


                columnNames.clear();
                //表头用一个vector容器存放
                columnNames.add("成绩表号");
                columnNames.add("学号");
                columnNames.add("课程号");
                columnNames.add("成绩");
                scs = initial.getSc(conn);//数据用vector<vector<>>存放

                //*********更新table表操作***********
                sc_table = new JTable(scs,columnNames);
                scrollPane3.setViewportView(sc_table);
                //*********更新table表操作***********

            }
        }catch (Exception e3){
            e3.printStackTrace();
        }
    }



    private void thisWindowActivated(WindowEvent e) {
        // TODO add your code here
    }

    private void thisWindowOpened(WindowEvent e) {
        // TODO add your code here
    }


    private void refreshGetInforMouseClicked(MouseEvent e) {
        setInformation();
    }

    private void student_tableMouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            int row = student_table.getSelectedRow();
            System.out.println(row);
        }
    }

    private void student_tableMousePressed(MouseEvent e) {
        System.out.println("按下");
    }

    private void scrollPane1MouseClicked(MouseEvent e) {
        System.out.println("按下");

    }

    private void add_buttonMouseClicked(MouseEvent e) {
        int flag = tabbedPane1.getSelectedIndex(); //0-学生管理 1-课程管理 2-成绩管理
        if (flag == 0){
            //进入学生信息添加界面
          JDialog stuDialog =  new addStudentDialog(StudentManager.this);
          stuDialog.setVisible(true);
        }else if(flag == 1){
            //进入课程信息添加界面
            JDialog courseDialog =  new addCourseDialog(StudentManager.this);
            courseDialog.setVisible(true);
        }else{
            //进入成绩表信息添加界面
            JDialog scDialog =  new addScDialog(StudentManager.this);
            scDialog.setVisible(true);
        }

    }

    private void delete_buttonMouseClicked(MouseEvent e) {
        if (tabbedPane1.getSelectedIndex() == 0){
            deleteStu();

        }else if(tabbedPane1.getSelectedIndex() == 1){
            deleteCourse();

        }else if(tabbedPane1.getSelectedIndex() == 2){
            deleteSc();

        }

    }

    void deleteStu(){
        String sno = (String)student_table.getValueAt(student_table.getSelectedRow(),0);
        String sql = "delete from student where sno=?";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,sno);
            pst.execute();
            setInformation();
        }catch (Exception e3){
            e3.printStackTrace();
        }
    }

    void deleteCourse(){
        String cno = (String)course_table.getValueAt(course_table.getSelectedRow(),0);
        String sql = "delete from Course where cno=?";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,cno);
            pst.execute();
            setInformation();
        }catch (Exception e3){
            e3.printStackTrace();
        }
    }

    void deleteSc(){
        String scno = (String)sc_table.getValueAt(sc_table.getSelectedRow(),0);
        String sql = "delete from Sc where scno=?";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,scno);
            pst.execute();
            setInformation();
        }catch (Exception e3){
            e3.printStackTrace();
        }
    }

    private void about_buttonMouseClicked(MouseEvent e) {
        Dialog dialog = new about(StudentManager.this);
        dialog.setVisible(true);
    }

    private void modify_buttonMouseClicked(MouseEvent e) {
        if (tabbedPane1.getSelectedIndex() == 0){
            String sno = (String)student_table.getValueAt(student_table.getSelectedRow(),0);
            String name = (String)student_table.getValueAt(student_table.getSelectedRow(),1);
            String sex = (String)student_table.getValueAt(student_table.getSelectedRow(),2);
            String birth = (String)student_table.getValueAt(student_table.getSelectedRow(),3);
            String mno = (String)student_table.getValueAt(student_table.getSelectedRow(),4);
            String grade = (String)student_table.getValueAt(student_table.getSelectedRow(),5);
            new Student(sno,name,sex,birth,mno,grade);//传值

            JDialog jDialog  = new addStudentDialog(StudentManager.this);
            jDialog.setVisible(true);
        }else if(tabbedPane1.getSelectedIndex() == 1){
            String  cno = (String) course_table.getValueAt(course_table.getSelectedRow(),0);
            String  pcno = (String) course_table.getValueAt(course_table.getSelectedRow(),1);
            String  cname = (String) course_table.getValueAt(course_table.getSelectedRow(),2);
            String  grade = (String) course_table.getValueAt(course_table.getSelectedRow(),3);
            String  score = (String) course_table.getValueAt(course_table.getSelectedRow(),4);
            new Course(cno,pcno,cname,grade,score);

            JDialog jDialog  = new addCourseDialog(StudentManager.this);
            jDialog.setVisible(true);
        }else if(tabbedPane1.getSelectedIndex() == 2){
            String  scno = (String) sc_table.getValueAt(sc_table.getSelectedRow(),0);
            String  sno = (String) sc_table.getValueAt(sc_table.getSelectedRow(),1);
            String  cno = (String) sc_table.getValueAt(sc_table.getSelectedRow(),2);
            String  mark = (String) sc_table.getValueAt(sc_table.getSelectedRow(),3);
            new Sc(scno,sno,cno,mark);
            JDialog jDialog  = new addScDialog(StudentManager.this);
            jDialog.setVisible(true);
        }


    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        tabbedPane1 = new JTabbedPane();
        student_panel = new JPanel();
        scrollPane1 = new JScrollPane();
        student_table = new JTable();
        course_panel = new JPanel();
        student_panel2 = new JPanel();
        scrollPane2 = new JScrollPane();
        course_table = new JTable();
        sc_panel = new JPanel();
        student_panel3 = new JPanel();
        scrollPane3 = new JScrollPane();
        sc_table = new JTable();
        menuBar1 = new JMenuBar();
        menu2 = new JMenu();
        refreshGetInfor = new JButton();
        modify_button = new JButton();
        delete_button = new JButton();
        add_button = new JButton();
        menuBar2 = new JMenuBar();
        menu3 = new JMenu();
        about_button = new JButton();

        //======== this ========
        setTitle("\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                thisWindowActivated(e);
                thisWindowActivated(e);
            }
            @Override
            public void windowOpened(WindowEvent e) {
                thisWindowOpened(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(null);

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //======== tabbedPane1 ========
                {

                    //======== student_panel ========
                    {
                        student_panel.setLayout(null);

                        //======== scrollPane1 ========
                        {
                            scrollPane1.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    scrollPane1MouseClicked(e);
                                }
                            });

                            //---- student_table ----
                            student_table.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    student_tableMouseClicked(e);
                                    student_tableMouseClicked(e);
                                }
                                @Override
                                public void mousePressed(MouseEvent e) {
                                    student_tableMousePressed(e);
                                }
                            });
                            scrollPane1.setViewportView(student_table);
                        }
                        student_panel.add(scrollPane1);
                        scrollPane1.setBounds(0, 0, 495, 290);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < student_panel.getComponentCount(); i++) {
                                Rectangle bounds = student_panel.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = student_panel.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            student_panel.setMinimumSize(preferredSize);
                            student_panel.setPreferredSize(preferredSize);
                        }
                    }
                    tabbedPane1.addTab("\u5b66\u751f\u7ba1\u7406", student_panel);

                    //======== course_panel ========
                    {
                        course_panel.setLayout(null);

                        //======== student_panel2 ========
                        {
                            student_panel2.setLayout(null);

                            //======== scrollPane2 ========
                            {

                                //---- course_table ----
                                course_table.addMouseListener(new MouseAdapter() {
                                    @Override
                                    public void mouseClicked(MouseEvent e) {
                                        student_tableMouseClicked(e);
                                    }
                                });
                                scrollPane2.setViewportView(course_table);
                            }
                            student_panel2.add(scrollPane2);
                            scrollPane2.setBounds(0, 0, 495, 305);

                            {
                                // compute preferred size
                                Dimension preferredSize = new Dimension();
                                for(int i = 0; i < student_panel2.getComponentCount(); i++) {
                                    Rectangle bounds = student_panel2.getComponent(i).getBounds();
                                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                                }
                                Insets insets = student_panel2.getInsets();
                                preferredSize.width += insets.right;
                                preferredSize.height += insets.bottom;
                                student_panel2.setMinimumSize(preferredSize);
                                student_panel2.setPreferredSize(preferredSize);
                            }
                        }
                        course_panel.add(student_panel2);
                        student_panel2.setBounds(0, 0, 490, 308);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < course_panel.getComponentCount(); i++) {
                                Rectangle bounds = course_panel.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = course_panel.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            course_panel.setMinimumSize(preferredSize);
                            course_panel.setPreferredSize(preferredSize);
                        }
                    }
                    tabbedPane1.addTab("\u8bfe\u7a0b\u7ba1\u7406", course_panel);

                    //======== sc_panel ========
                    {
                        sc_panel.setLayout(null);

                        //======== student_panel3 ========
                        {
                            student_panel3.setLayout(null);

                            //======== scrollPane3 ========
                            {

                                //---- sc_table ----
                                sc_table.addMouseListener(new MouseAdapter() {
                                    @Override
                                    public void mouseClicked(MouseEvent e) {
                                        student_tableMouseClicked(e);
                                    }
                                });
                                scrollPane3.setViewportView(sc_table);
                            }
                            student_panel3.add(scrollPane3);
                            scrollPane3.setBounds(0, 0, 495, 305);

                            {
                                // compute preferred size
                                Dimension preferredSize = new Dimension();
                                for(int i = 0; i < student_panel3.getComponentCount(); i++) {
                                    Rectangle bounds = student_panel3.getComponent(i).getBounds();
                                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                                }
                                Insets insets = student_panel3.getInsets();
                                preferredSize.width += insets.right;
                                preferredSize.height += insets.bottom;
                                student_panel3.setMinimumSize(preferredSize);
                                student_panel3.setPreferredSize(preferredSize);
                            }
                        }
                        sc_panel.add(student_panel3);
                        student_panel3.setBounds(0, 0, 490, 308);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < sc_panel.getComponentCount(); i++) {
                                Rectangle bounds = sc_panel.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = sc_panel.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            sc_panel.setMinimumSize(preferredSize);
                            sc_panel.setPreferredSize(preferredSize);
                        }
                    }
                    tabbedPane1.addTab("\u6210\u7ee9\u8868", sc_panel);
                }
                contentPanel.add(tabbedPane1);
                tabbedPane1.setBounds(0, 5, 490, 320);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel);
            contentPanel.setBounds(5, 20, 500, 325);

            //======== menuBar1 ========
            {

                //======== menu2 ========
                {
                    menu2.setText("\u529f\u80fd");

                    //---- refreshGetInfor ----
                    refreshGetInfor.setText("\u5237\u65b0\u6240\u6709");
                    refreshGetInfor.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            refreshGetInforMouseClicked(e);
                        }
                    });
                    menu2.add(refreshGetInfor);

                    //---- modify_button ----
                    modify_button.setText("\u4fee\u6539\u8be5\u884c");
                    modify_button.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            modify_buttonMouseClicked(e);
                        }
                    });
                    menu2.add(modify_button);

                    //---- delete_button ----
                    delete_button.setText("\u5220\u9664\u8be5\u884c");
                    delete_button.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            delete_buttonMouseClicked(e);
                        }
                    });
                    menu2.add(delete_button);

                    //---- add_button ----
                    add_button.setText("\u6dfb\u52a0\u6570\u636e");
                    add_button.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            add_buttonMouseClicked(e);
                        }
                    });
                    menu2.add(add_button);
                }
                menuBar1.add(menu2);
            }
            dialogPane.add(menuBar1);
            menuBar1.setBounds(new Rectangle(new Point(5, 0), menuBar1.getPreferredSize()));

            //======== menuBar2 ========
            {

                //======== menu3 ========
                {
                    menu3.setText("\u5173\u4e8e");

                    //---- about_button ----
                    about_button.setText("\u5173\u4e8e\u4f5c\u8005");
                    about_button.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            about_buttonMouseClicked(e);
                        }
                    });
                    menu3.add(about_button);
                }
                menuBar2.add(menu3);
            }
            dialogPane.add(menuBar2);
            menuBar2.setBounds(new Rectangle(new Point(45, 0), menuBar2.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialogPane.getComponentCount(); i++) {
                    Rectangle bounds = dialogPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialogPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialogPane.setMinimumSize(preferredSize);
                dialogPane.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(dialogPane);
        dialogPane.setBounds(0, 0, 505, 350);

        contentPane.setPreferredSize(new Dimension(510, 380));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents


    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JTabbedPane tabbedPane1;
    private JPanel student_panel;
    private JScrollPane scrollPane1;
    private JTable student_table;
    private JPanel course_panel;
    private JPanel student_panel2;
    private JScrollPane scrollPane2;
    private JTable course_table;
    private JPanel sc_panel;
    private JPanel student_panel3;
    private JScrollPane scrollPane3;
    private JTable sc_table;
    private JMenuBar menuBar1;
    private JMenu menu2;
    private JButton refreshGetInfor;
    private JButton modify_button;
    private JButton delete_button;
    private JButton add_button;
    private JMenuBar menuBar2;
    private JMenu menu3;
    private JButton about_button;
    // JFormDesigner - End of variables declaration  //GEN-END:variables


}
